using System.Collections.Generic;
using Oracle.ManagedDataAccess.Client;
using OracleInternal.Common;

namespace OracleInternal.BulkCopy
{
	internal class DirectPathStreamPlanner
	{
		internal ColumnDescribeInfo[] m_colsMetaData;

		internal OracleParameterCollection m_colParameters;

		internal object[] m_colParamValueArray;

		internal bool m_bAllFastTypes;

		internal List<DirectPathRow> m_rows;

		internal static int SizeOfColumnLength(bool bIsNull, bool bIsEmpty, int colDataLen)
		{
			if (bIsNull || bIsEmpty)
			{
				return 1;
			}
			if (colDataLen > 250)
			{
				return 3;
			}
			return 1;
		}

		internal static bool IsLargeColumn(int oraType)
		{
			if (oraType != 8 && oraType != 24 && oraType != 113 && oraType != 112)
			{
				return oraType == 119;
			}
			return true;
		}

		internal static DirectPathRow CreateDirectPathRow(bool bAllFastColumns, BulkCopyBindingRow bindingRow)
		{
			DirectPathRow fromCache = DirectPathRow.GetFromCache(bAllFastColumns);
			int num = 0;
			int num2 = 0;
			int num3 = 0;
			int num4 = 0;
			int num5 = 0;
			int num6 = 0;
			int num7 = 0;
			int num8 = 0;
			while (num < bindingRow.m_columnCount)
			{
				if (bindingRow.m_col_nullIndicators[num])
				{
					num3 = 0;
					num5 = SizeOfColumnLength(bIsNull: true, bIsEmpty: false, 0);
				}
				else if (bindingRow.m_bindValuesLen[num] == 0)
				{
					num3 = 0;
					num5 = SizeOfColumnLength(bIsNull: false, bIsEmpty: true, 0);
				}
				else
				{
					num3 = bindingRow.m_bindValuesLen[num];
					num5 = SizeOfColumnLength(bIsNull: false, bIsEmpty: false, num3);
				}
				num4 = num3 + num5;
				if (num4 + num2 < 65513)
				{
					if (num == bindingRow.m_columnCount - 1)
					{
						num2 += num4;
						num8 = num;
						num7++;
						if (num6 == 0)
						{
							fromCache.AddPiece(DirectPathRowPiece.CreateFullPiece(bAllFastColumns, num2, num6, 0, num8, num3 - 1));
						}
						else
						{
							fromCache.AddPiece(DirectPathRowPiece.CreateLastPiece(bAllFastColumns, num2, num6, 0, num8, num3 - 1));
						}
						num++;
					}
					else
					{
						num++;
						num7++;
						num2 += num4;
					}
				}
				else if (num7 > 0)
				{
					num8 = num - 1;
					if (num6 == 0)
					{
						fromCache.AddPiece(DirectPathRowPiece.CreateFirstPiece(bAllFastColumns, num2, num6, 0, num8, bindingRow.m_bindValuesLen[num8] - 1));
					}
					else
					{
						fromCache.AddPiece(DirectPathRowPiece.CreateNextPiece(bAllFastColumns, num2, num6, 0, num8, bindingRow.m_bindValuesLen[num8] - 1));
					}
					num2 = 0;
					num6 = num;
					num7 = 0;
				}
				else
				{
					CreateRowPieceForLargeSizeColumn(fromCache, bAllFastColumns, num, num3, num == bindingRow.m_columnCount - 1);
					num++;
					num2 = 0;
					num6 = num;
					num7 = 0;
				}
			}
			return fromCache;
		}

		internal static int CreateRowPieceForLargeSizeColumn(DirectPathRow row, bool bAllFastColumns, int colIndex, int colLen, bool bLastCol)
		{
			int num = 0;
			int num2 = 0;
			int num3 = 0;
			int num4 = colLen;
			int num5 = 0;
			int num6 = 0;
			bool flag = true;
			while (num4 > 0)
			{
				if (num4 > 65513)
				{
					num2 = 65513;
					num3 = num2 + 3;
					num6 = num5 + num2 - 1;
					if (flag)
					{
						row.AddPiece(DirectPathRowPiece.CreateFirstSplitPiece(bAllFastColumns, num3, colIndex, num5, num6));
						num++;
						flag = false;
					}
					else
					{
						row.AddPiece(DirectPathRowPiece.CreateNextSplitPiece(bAllFastColumns, num3, colIndex, num5, num6));
						num++;
					}
					num4 -= num2;
					num5 = num6 + 1;
				}
				else
				{
					num2 = num4;
					num3 = num2 + SizeOfColumnLength(bIsNull: false, bIsEmpty: false, num2);
					num6 = num5 + num2 - 1;
					row.AddPiece(DirectPathRowPiece.CreateLastSplitPiece(bAllFastColumns, num3, colIndex, num5, num6, bLastCol));
					num++;
					num4 -= num2;
				}
			}
			return num;
		}
	}
}
